require.config({
	paths:{
		"jquery":"jquery-1.11.3",
		"cookie":"jquery.cookie"
	}
})
require(["jquery","cookie"],function($,cookie){
	$(function(){
		$(".phone-btn").click(function(){
			var user = $(".username").val();
			var pwd = $(".password").val();
			
			if(!user){
				alert("用户名不能为空");
				return;
			}
			
			//获取cookie中的信息
			var users = $.cookie("registerUsers") ? $.cookie("registerUsers") : "";
			users = convertStrToObj(users);
			if(user in users){
				alert("用户名已注册");
				return;
			}else{
				users[user] = pwd;
				userStr = convertObjToStr(users);
				$.cookie("registerUsers",userStr,{expires:7,path:"/"});
				alert("注册成功！");
			}
		});
		$(".login").click(function(){
			location.href = "login.html";
		});
	})
	//将字符串转为对象
	function convertStrToObj(str){
		if(!str){
			return{};
		}
		var users = str.split(":");
		var obj = {};
		for(var i = 0; i < users.length; i ++){
			var userData = users[i].split(",");
			obj[userData[0]] = userData[1];//对象的
		}
		return obj;
	}
//	将对象转为字符串
	function convertObjToStr(obj){
		var str = "";
		for(var user in obj){
			var pwd = obj[user];
			if(str){
				str += ":";
			}
			str += user + "," + pwd;
		}
		return str;
	}
})